home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Dialog.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  273 lines

  1. /*
  2.  * @(#)Dialog.java    1.42 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.DialogPeer;
  17. import java.awt.event.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21.  
  22.  
  23. /**
  24.  * A class that produces a dialog - a window that takes input from the user.
  25.  * The default layout for a dialog is BorderLayout.
  26.  * <p>
  27.  * Dialogs are capable of generating the following window events:
  28.  * WindowOpened, WindowClosing, WindowClosed, WindowActivated, WindowDeactivated.
  29.  * @see WindowEvent
  30.  * @see Window#addWindowListener
  31.  *
  32.  * @version     1.38, 12/03/97
  33.  * @author     Sami Shaio
  34.  * @author     Arthur van Hoff
  35.  * @since       JDK1.0
  36.  */
  37. public class Dialog extends Window {
  38.     boolean    resizable = true;
  39.  
  40.     /**
  41.      * Sets to true if the Dialog is modal.  A modal
  42.      * Dialog grabs all the input to the parent frame from the user.
  43.      */
  44.     boolean modal;
  45.  
  46.     /**
  47.      * The title of the Dialog.
  48.      */
  49.     String title;
  50.  
  51.     private static final String base = "dialog";
  52.     private static int nameCounter = 0;
  53.  
  54.     /*
  55.      * JDK 1.1 serialVersionUID 
  56.      */
  57.     private static final long serialVersionUID = 5920926903803293709L;
  58.  
  59.     /**
  60.      * Constructs an initially invisible Dialog with an empty title.
  61.      * @param parent the owner of the dialog
  62.      * @see Component#setSize
  63.      * @see Component#setVisible
  64.      * @since JDK1.0
  65.      */
  66.     public Dialog(Frame parent) {
  67.     this(parent, "", false);
  68.     }
  69.  
  70.     /**
  71.      * Constructs an initially invisible Dialog with an empty title.
  72.      * A modal Dialog grabs all the input to the parent frame from the user.
  73.      * @param parent the owner of the dialog
  74.      * @param modal if true, dialog blocks input to the parent window when shown
  75.      */
  76.     public Dialog(Frame parent, boolean modal) {
  77.     this(parent, "", modal);
  78.     }
  79.  
  80.     /**
  81.      * Constructs an initially invisible Dialog with a title. 
  82.      * @param parent the owner of the dialog
  83.      * @param title the title of the dialog
  84.      * @see Component#setSize
  85.      * @see Component#setVisible
  86.      * @since JDK1.0
  87.      */
  88.     public Dialog(Frame parent, String title) {
  89.     this(parent, title, false);
  90.     }
  91.  
  92.     /**
  93.      * Constructs an initially invisible Dialog with a title. 
  94.      * A modal Dialog grabs all the input to the parent frame from the user.
  95.      * @param parent the owner of the dialog
  96.      * @param title the title of the dialog
  97.      * @param modal if true, dialog blocks input to the parent window when shown
  98.      * @see Component#setSize
  99.      * @see Component#setVisible
  100.      * @since JDK1.0
  101.      */
  102.     public Dialog(Frame parent, String title, boolean modal) {
  103.     super(parent);
  104.     if (parent == null) {
  105.         throw new IllegalArgumentException("null parent frame");
  106.     }
  107.     this.title = title;
  108.     this.modal = modal;
  109.     }
  110.  
  111.     /**
  112.      * Construct a name for this component.  Called by getName() when the
  113.      * name is null.
  114.      */
  115.     String constructComponentName() {
  116.         return base + nameCounter++;
  117.     }
  118.  
  119.     /**
  120.      * Creates the dialog's peer.  The peer allows us to change the appearance
  121.      * of the frame without changing its functionality.
  122.      * @since JDK1.0
  123.      */
  124.     public void addNotify() {
  125.         synchronized (getTreeLock()) {
  126.             if (peer == null) {
  127.                 peer = getToolkit().createDialog(this);
  128.             }
  129.             super.addNotify();
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Indicates whether the dialog is modal.  
  135.      * A modal dialog grabs all input from the user.
  136.      * @return    <code>true</code> if this dialog window is modal; 
  137.      *            <code>false</code> otherwise. 
  138.      * @see       java.awt.Dialog#setModal    
  139.      * @since     JDK1.0
  140.      */
  141.     public boolean isModal() {
  142.     return modal;
  143.     }
  144.  
  145.     /**
  146.      * Specifies whether this dialog is modal.  A modal
  147.      * Dialog grabs all the input to the parent frame from the user.
  148.      * @see       java.awt.Dialog#isModal 
  149.      * @since     JDK1.1
  150.      */
  151.     public void setModal(boolean b) {
  152.     this.modal = b;
  153.     }
  154.  
  155.     /**
  156.      * Gets the title of the dialog.
  157.      * @return    the title of this dialog window.
  158.      * @see       java.awt.Dialog#setTitle
  159.      * @since     JDK1.0
  160.      */
  161.     public String getTitle() {
  162.     return title;
  163.     }
  164.  
  165.     /**
  166.      * Sets the title of the Dialog.
  167.      * @param title the new title being given to the dialog
  168.      * @see #getTitle
  169.      * @since JDK1.0
  170.      */
  171.     public synchronized void setTitle(String title) {
  172.     this.title = title;
  173.     DialogPeer peer = (DialogPeer)this.peer;
  174.     if (peer != null) {
  175.         peer.setTitle(title);
  176.     }
  177.     }
  178.  
  179.    /**
  180.      * Shows the dialog. This will bring the dialog to the
  181.      * front if the dialog is already visible.  If the dialog is
  182.      * modal, this call will block input to the parent window
  183.      * until the dialog is taken down
  184.      * by calling hide or dispose. It is permissible to show modal
  185.      * dialogs from the event dispatching thread because the toolkit
  186.      * will ensure that another dispatching thread will run while
  187.      * the one which invoked show is blocked. 
  188.      * @see Component#hide
  189.      * @since JDK1.0
  190.      */
  191.     public void show() {
  192.       synchronized(getTreeLock()) {
  193.         if (parent != null && parent.getPeer() == null) {
  194.             parent.addNotify();
  195.         }
  196.     if (peer == null) {
  197.         addNotify();
  198.     }
  199.       }
  200.     validate();        
  201.     if (visible) {
  202.         toFront();
  203.     } else {
  204.         visible = true;
  205.         if (isModal()) {
  206.                 EventDispatchThread dt = null;
  207.                 if (Thread.currentThread() instanceof EventDispatchThread) {
  208.                 dt = new EventDispatchThread("AWT-Dispatch-Proxy", 
  209.                                                  Toolkit.getEventQueue());
  210.                     dt.start();
  211.                 }
  212.                 // For modal case, we most post this event before calling
  213.                 // show, since calling peer.show() will block; this is not
  214.                 // ideal, as the window isn't yet visible on the screen...
  215.                 if ((state & OPENED) == 0) {
  216.                     postWindowEvent(WindowEvent.WINDOW_OPENED);
  217.                     state |= OPENED;
  218.                 }
  219.         peer.show(); // blocks until dialog brought down
  220.                 if (dt != null) {
  221.                     dt.stopDispatching();
  222.                 }                
  223.         } else {
  224.         peer.show();
  225.                 if ((state & OPENED) == 0) {
  226.                     postWindowEvent(WindowEvent.WINDOW_OPENED);
  227.                     state |= OPENED;
  228.                 }
  229.         }
  230.     }
  231.     }
  232.  
  233.     /**
  234.      * Indicates whether this dialog window is resizable.
  235.      * @return    <code>true</code> if the user can resize the dialog;
  236.      *            <code>false</code> otherwise.
  237.      * @see       java.awt.Dialog#setResizable
  238.      * @since     JDK1.0
  239.      */
  240.     public boolean isResizable() {
  241.     return resizable;
  242.     }
  243.  
  244.     /**
  245.      * Sets the resizable flag.
  246.      * @param     resizable <code>true</code> if the user can 
  247.      *                 resize this dialog; <code>false</code> otherwise.
  248.      * @see       java.awt.Dialog#isResizable
  249.      * @since     JDK1.0
  250.      */
  251.     public synchronized void setResizable(boolean resizable) {
  252.     this.resizable = resizable;
  253.     DialogPeer peer = (DialogPeer)this.peer;
  254.     if (peer != null) {
  255.         peer.setResizable(resizable);
  256.     }
  257.     }
  258.  
  259.     /**
  260.      * Returns the parameter string representing the state of this 
  261.      * dialog window. This string is useful for debugging. 
  262.      * @return    the parameter string of this dialog window.
  263.      * @since     JDK1.0
  264.      */
  265.     protected String paramString() {
  266.     String str = super.paramString() + (modal ? ",modal" : ",modeless");
  267.     if (title != null) {
  268.         str += ",title=" + title;
  269.     }
  270.     return str;
  271.     }
  272. }
  273.